//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using System;
using Codingly.SvnExternalUpdater.Core.EventHandlers;
using Codingly.SvnExternalUpdater.Core.Model;
using Codingly.SvnExternalUpdater.Core.Services;
using Codingly.SvnExternalUpdater.Mvp;
using Codingly.SvnExternalUpdater.Views;

namespace Codingly.SvnExternalUpdater.Presenters
{
	public class ExternalPresenter : AbstractPresenter<IExternalView, ExternalPresenter>
	{
		private readonly IMonitoringService monitoringService;
		private readonly ITortoiseService tortoiseService;
		private IExternal external;
		private string targetPlaceHolderKey;

		public ExternalPresenter(IMonitoringService monitoringService, ITortoiseService tortoiseService)
		{
			this.monitoringService = monitoringService;
			this.tortoiseService = tortoiseService;
		}

		public void Pause()
		{
		}

		public void Resume()
		{
		}

		public void ShowLog()
		{
			this.tortoiseService.ShowLog(this.external);
		}

		public void Start(IExternal external, string targetPlaceHolderKey)
		{
			this.targetPlaceHolderKey = targetPlaceHolderKey;
			this.external = external;
			this.external.Changed += external_Changed;
			this.view.Append(this.targetPlaceHolderKey);
			UpdateView();
		}

		public void Stop()
		{
			this.external.Changed -= external_Changed;
			this.view.Remove(this.targetPlaceHolderKey);
		}

		public void UpdateExternalRevision()
		{
			this.monitoringService.UpdateExternalRevision(this.external);
		}

		public void UpdateView()
		{
			this.view.LocalRevision = this.external.HasRevision ? this.external.LocalRevision.ToString() : "Head";
			this.view.RemoteRevision = this.external.RemoteRevision.ToString();
			this.view.IsUpToDate = this.external.IsUpToDate;
			this.view.ProjectName = this.external.Path;
		}

		private void external_Changed(object sender, ExternalEventArgs args)
		{
			UpdateView();
		}

		public event EventHandler ExternalRevisionUdpated = delegate { };
	}
}